<?php

/**
 * Example Controller
 *
 * PHP version 5
 *
 * @category Controller
 * @package  Croogo
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class AreasController extends AppController {

        /**
         * Controller name
         *
         * @var string
         * @access public
         */
        public $name = 'Areas';

        /**
         * Components
         *
         * @var array
         * @access public
         */
        public $components = array(
            'Search.Prg',
            'Uploader.Upload'
        );

        /**
         * Preset Variables Search
         *
         * @var array
         * @access public
         */
        public $presetVars = array(
            'name' => array('type' => 'value'),
        );

        /**
         * Models used by the Controller
         *
         * @var array
         * @access public
         */
        public $uses = array('Countries.Area');

        /**
         * beforeFilter
         *
         * @return void
         * @access public
         */
        public function beforeFilter() {
                parent::beforeFilter();
                if (in_array($this->request->action, array('admin_toggle'))) {
                        $this->Security->unlockedActions = $this->request->action;
                }
        }

        /**
         * Toggle Countries status
         *
         * @param $id string Area id
         * @param $status integer Current Area status
         * @return void
         */
        public function admin_toggle($id = null, $status = null) {
                $this->Croogo->fieldToggle($this->Area, $id, $status);
        }

        /**
         * admin_index
         *
         * @return void
         */
        public function admin_index() {
                $this->set('title_for_layout', __('Areas'));
                $this->Prg->commonProcess();
                $searchFields = array('name');

                $this->Area->recursive = 0;
                $this->paginate['Area']['order'] = 'Area.weight ASC';
                $this->set('areas', $this->paginate($this->Area->parseCriteria($this->passedArgs)));
                $this->set('searchFields', $searchFields);
        }

        /**
         * Admin add
         *
         * @return void
         * @access public
         */
        public function admin_add() {
                $this->set('title_for_layout', __('Add Area'));

                if (!empty($this->request->data)) {
                        $this->Area->create();
                        if ($this->Area->save($this->request->data)) {
                                $this->Session->setFlash(__('The Area has been saved'), 'default', array('class' => 'success'));
                                $this->Croogo->redirect(array('action' => 'edit', $this->Area->id));
                        } else {
                                $this->Session->setFlash(__('The Area could not be saved. Please, try again.'), 'default', array('class' => 'error'));
                        }
                }
        }

        /**
         * Admin edit
         *
         * @param integer $id
         * @return void
         * @access public
         */
        public function admin_edit($id = null) {
                $this->set('title_for_layout', __('Edit Area'));

                if (!$id && empty($this->request->data)) {
                        $this->Session->setFlash(__('Invalid Area'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }

                if (!empty($this->request->data)) {
                        if ($this->Area->save($this->request->data)) {
                                $this->Session->setFlash(__('The Area has been saved'), 'default', array('class' => 'success'));
                                $this->Croogo->redirect(array('action' => 'edit', $this->Area->id));
                        } else {
                                $this->Session->setFlash(__('The Area could not be saved. Please, try again.'), 'default', array('class' => 'error'));
                        }
                }
                if (empty($this->request->data)) {
                        $this->request->data = $this->Area->read(null, $id);
                }
        }

        /**
         * Admin delete
         *
         * @param integer $id
         * @return void
         * @access public
         */
        public function admin_delete($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid id for Area'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }
                if ($this->Area->delete($id)) {
                        $this->Session->setFlash(__('Area deleted'), 'default', array('class' => 'success'));
                        $this->redirect(array('action' => 'index'));
                }
        }

        /**
         * Admin moveup
         *
         * @param integer $id
         * @param integer $step
         * @return void
         * @access public
         */
        public function admin_moveup($id, $step = 1) {
                if ($this->Area->moveUp($id, $step)) {
                        $this->Session->setFlash(__('Moved up successfully'), 'default', array('class' => 'success'));
                } else {
                        $this->Session->setFlash(__('Could not move up'), 'default', array('class' => 'error'));
                }

                $this->redirect(array('action' => 'index'));
        }

        /**
         * Admin movedown
         *
         * @param integer $id
         * @param integer $step
         * @return void
         * @access public
         */
        public function admin_movedown($id, $step = 1) {
                if ($this->Area->moveDown($id, $step)) {
                        $this->Session->setFlash(__('Moved down successfully'), 'default', array('class' => 'success'));
                } else {
                        $this->Session->setFlash(__('Could not move down'), 'default', array('class' => 'error'));
                }

                $this->redirect(array('action' => 'index'));
        }

        /**
         * Admin process
         *
         * @return void
         * @access public
         */
        public function admin_process() {
                $action = $this->request->data['Area']['action'];
                $ids = array();
                foreach ($this->request->data['Area'] as $id => $value) {
                        if ($id != 'action' && $value['id'] == 1) {
                                $ids[] = $id;
                        }
                }

                if (count($ids) == 0 || $action == null) {
                        $this->Session->setFlash(__('No items selected.'), 'default', array('class' => 'error'));
                        $this->redirect(array('action' => 'index'));
                }

                if ($action == 'delete' &&
                        $this->Area->deleteAll(array('Area.id' => $ids), true, true)) {
                        $this->Session->setFlash(__('Area deleted'), 'default', array('class' => 'success'));
                } elseif ($action == 'publish' &&
                        $this->Area->updateAll(array('Area.status' => true), array('Area.id' => $ids))) {
                        $this->Session->setFlash(__('Areas published'), 'default', array('class' => 'success'));
                } elseif ($action == 'unpublish' &&
                        $this->Area->updateAll(array('Area.status' => false), array('Area.id' => $ids))) {
                        $this->Session->setFlash(__('Areas unpublished'), 'default', array('class' => 'success'));
                } else {
                        $this->Session->setFlash(__('An error occurred.'), 'default', array('class' => 'error'));
                }

                $this->redirect(array('action' => 'index'));
        }

        /**
         * index
         *
         * @return void
         */
        public function index() {
                $this->set('title_for_layout', __('Countries'));
                $this->set('exampleVariable', 'value here');
        }

        public function loadArea() {
                $this->autoRender = false;
                return $this->Area->loadArea($this->request->query['country_id']);
        }

        public function view() {
                $this->layout = 'city';
                $city = $this->Area->findByAlias($this->request->slug);
                $this->set('title_for_layout', __('Area:').$city['Area']['name']);
                $this->set(compact('city'));
        }

}
